package implementation;
import GameData.*;

import protocol.Query;
import protocol.State;
import protocol.StateObject;

public class PriceHandler {

	public static void handlePrice(StateObject nextState, Query query) {
		
		// Generate the intial prompt:
		// - "The best available set of $numtix tickets are $location, section $sectionnum, for $price dollars each."
		
		/*numTix, ticketType, section, sectionNum, price*/
		
		//Look up default game (i.e. next game) to supply default'ed info to user
		System.out.println("query.getGameInfo() is 1: " + query.getGameInfo());
		TicketAggregate ta = query.getGameInfo().getTicketInfo(
				Integer.parseInt(query.getNumTix()), "best available");
		System.out.println("query.getGameInfo() is 2: " + query.getGameInfo());
		query.setTicketInfo(ta);
		//Game g = query.getGameInfo();
		String standardPrompt = createStandardPrompt(query.getNumTix(),
				query.getPrice(), ta.section, 
				Integer.toString(ta.sectionNum),
				Integer.toString(ta.costPerTicket));
		String helpPrompt = "SpeakTicketsHelp";
		
		//Play prompt
		ExecUtils.playPrompt(standardPrompt, nextState);
		
		//Now get user input about the PRICE TYPE (i.e cheapest available)
		boolean receivedInput = false;
		while(!receivedInput)
		{
			ExecUtils.getUserInput(query, "gram_price");
			
			/*
			 * Check if PRICE is an input
			 */
			if(query.priceIsNew())
			{
				TicketAggregate tempTa = query.getGameInfo().getTicketInfo(
						Integer.parseInt(query.getNumTix()), query.getPrice());
				query.setTicketInfo(tempTa);
				System.out.println("query.getGameInfo() is 3: " + query.getGameInfo());
				standardPrompt = createStandardPrompt2(query.getNumTix(),
						new String(query.getPrice()), tempTa.section, 
						Integer.toString(tempTa.sectionNum), 
						Integer.toString(tempTa.costPerTicket));
				ExecUtils.playPrompt(standardPrompt, nextState);
						System.out.println("******PRICE IS NEW");
			}
			
			/*
			 * Check for SHORT CONFIRMATIONS
			 * - play HELP if PRICE is null
			 */
			else if(query.getShortConf() != null)
			{
				System.out.println("SHORT CONF WAS NOT NULL");
				query.voidTransaction();
				String shortResponse = query.getShortConf().trim().toLowerCase();
				if(shortResponse.equals("start over") ||
						shortResponse.equals("restart") ||
						shortResponse.equals("startover"))
				{
					receivedInput = true;
					ExecUtils.playPrompt(shortResponse, nextState);
					nextState.setState(State.NUMTIX);
				}
				else if(shortResponse.equals("go back") ||
						shortResponse.equals("goback"))
				{
					receivedInput = true;
					ExecUtils.playPrompt(shortResponse, nextState);
					nextState.setState(State.AWAYTEAMCONF);
				}
				else if(shortResponse.equals("purchase"))
				{
					receivedInput = true;
					nextState.setState(State.RECAP);
				}
				else
				{
					ExecUtils.playPrompt(helpPrompt, nextState);
				}
			}
			else{
				query.voidTransaction();
				ExecUtils.playPrompt(helpPrompt, nextState);
			}
		}
	}
	
	/**
	 * Create the PRICE prompt to the user, values can be omitted but then will
	 * be filled in with incorrect data.
	 * @param numTix
	 * @param ticketType
	 * @param section
	 * @param sectionNum
	 * @param price
	 * @return
	 */
	private static String createStandardPrompt(String numTix, String ticketType,
			String section, String sectionNum, String price)
	{
		return "SpeakTickets%" +
			(numTix!=null?numTix:"2") + "," +
			(ticketType!=null?ticketType:"best available") + "," +
			(section!=null?section:"field level")  + "," +
			(sectionNum!=null?sectionNum:"24") + "," +
			(price!=null?price:"25");
	}
	
	private static String createStandardPrompt2(String numTix, String ticketType,
			String section, String sectionNum, String price)
	{
		return "SpeakTickets2%" +
			(numTix!=null?numTix:"2") + "," +
			(ticketType!=null?ticketType:"best available") + "," +
			(section!=null?section:"field level")  + "," +
			(sectionNum!=null?sectionNum:"24") + "," +
			(price!=null?price:"25");
	}
}
